package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Menu2;
import com.itheima.pojo.Role;
import com.itheima.service.MemberService;
import com.itheima.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

import java.util.*;

/**
 * @author liam
 * @description 用户控制器
 * @date 2020/3/5-16:31
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Reference
    MemberService memberService;

    @RequestMapping("/getUsername")
    public Result getUsername() {
        //此处是从spring-security上下文中获取目前登录的user对象，注意：是spring的user
        org.springframework.security.core.userdetails.User user =
                (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return Result.success("", user.getUsername());
    }

    @RequestMapping("/findPage")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        return  userService.findPage(queryPageBean);
    }

    @RequestMapping("/add")
    @PreAuthorize("hasAuthority('USER_ADD')")
    public Result add(@RequestBody User user) {
        //将密码加密后存入用户对象
        String encodePassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodePassword);
        userService.add(user);
        return Result.success("新增用户成功");
    }

    @RequestMapping("/findById4Edit")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result findById4Edit(Integer id) {
        Map<String,Object> map = userService.findById4Edit(id);
        return Result.success("", map);
    }

    @RequestMapping("/edit")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result edit(@RequestBody User user) {
        userService.edit(user);
        return Result.success("更新用户成功");
    }

    @RequestMapping("/delete")
    @PreAuthorize("hasAuthority('USER_EDIT')")
    public Result delete(Integer id) {
        userService.delete(id);
        return Result.success("删除用户成功");
    }
    @RequestMapping("/getMenuList")
    public Result getMenuList() {
        try {
            Map map = new HashMap();
            org.springframework.security.core.userdetails.User user = (org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            com.itheima.pojo.User healthUser = userService.findByUsername(user.getUsername());
            Set<Role> roles = healthUser.getRoles();
            if (CollectionUtil.isNotEmpty(roles)) {
                List<Integer> roleIds = new ArrayList<>();
                for (Role role : roles) {
                    roleIds.add(role.getId());
                }
                List<Menu2> menu2s = memberService.getMenuList(roleIds);
                map.put("menuList", menu2s);
            }
            map.put("username", user.getUsername());
            return new Result(true, "成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "失败");
        }
    }

}
